#!/bin/bash
set -e

# Find the --sysroot argument
sysroot=
next_arg=false
for arg; do
	if ${next_arg}; then
		next_arg=false
		sysroot="${arg}"
		continue  # not break, in case there are more than one
	fi
	case "${arg}" in
	(--sysroot|-r)
		next_arg=true
		continue
		;;
	(--sysroot=*)
		sysroot="${arg#*=}"
		continue  # not break, in case there are more than one
		;;
	(-r?*)
		sysroot="${arg#-r}"
		continue  # not break, in case there are more than one
		;;
	esac
done
if [ -z "${sysroot}" ]; then
	echo "${0}: --sysroot argument must be given." 1>&2
	exit 1
fi

topdir="$(dirname "$(realpath "$(dirname "${0}")")")"
DRACUT_LDD="$(mktemp /tmp/dracut-ldd.XXXXXX)"
cat >"${DRACUT_LDD}" <<EOL
#!/bin/bash
${topdir}/sbin/prelink-rtld --root='${sysroot}' \${1}
EOL
chmod +x "${DRACUT_LDD}"
export DRACUT_LDD
export DRACUT_INSTALL="${topdir}/lib/dracut/dracut-install"
export DRACUT_LDCONFIG=/bin/true
export dracutbasedir="${topdir}/lib/dracut"
(exec "${topdir}/bin/dracut.real" "${@}")

if [ -n "${DRACUT_LDD}" ]; then
	rm -f "${DRACUT_LDD}"
fi
